<template>
	<Layout class="p-5 bg-white">
		<h1 style="text-align: center;margin: 20px;">买车发布</h1>
		<Form :label-width="150" style="margin-left: 650px;">

			<FormItem class="w-50" label="标题">
				<Input v-model="carRentClientVo.title" style="width:300px" placeholder="请输入标题"></Input>
			</FormItem>
			<FormItem class="w-50" label="电话号">
				<Input v-model="carRentClientVo.tel" style="width:300px" placeholder="请输入电话号"></Input>
			</FormItem>
			<FormItem class="w-50" label="微信号码">
				<Input v-model="carRentClientVo.wxNumber" style="width:300px" placeholder="请输入微信号"></Input>(非必填)
			</FormItem>
			<FormItem class="w-50" label="车辆详情">
				<Input v-model="carRentClientVo.detail" type="textarea" style="width:300px"
					placeholder="请输入车辆详情"></Input>
			</FormItem>
			<FormItem class="text-right">
				<Button @click="submit" type="primary" v-if="sub">提交</Button>
				<Button @click="modal" v-if="pay">支付</Button>
				<Modal v-model="carRentClientVo.modal11" title="支付">
					<img :src="carRentClientVo.url" />
				</Modal>
				<Button @click="cancel" class="ml-2">取消</Button>
			</FormItem>
		</Form>
	</Layout>
</template>

<script>
	export default {
		data() {
			return {
				iport: "",
				sub: false,
				pay: false,
				number: 10,
				longSearch: "",
				orderInfo: {},
				currentUser: {},
				carRentClientVo: {
					url: '',
					modal11: false,
					title: "",
					detail: "",
					tel: '',
					wxNumber: '',
					clientId: ''
				},
			}
		},
		created: function() {
			this.iport = this.store.state.iport
			this.$data.currentUser = JSON.parse(sessionStorage.getItem('CURRENT'))
			this.$data.carRentClientVo.clientId = this.$data.currentUser.client.clientId
			this.http.get({
				//查看已支付未操作的订单
				url: '/pay-order/list',
				param: {
					clientId: this.$data.carRentClientVo.clientId,
					orderTypeId: 7,
				},
			}).then((data) => {
				this.$data.orderInfo = data;
				if (this.$data.orderInfo) {
					this.sub = true
				} else {
					this.http.post({
						//新增订单
						url: '/pay-order/info',
						param: {
							clientId: this.$data.carRentClientVo.clientId,
							orderTypeId: 7,
						}
					}).then((data) => {
						this.$data.orderInfo = data
						this.pay = true
					})
				}
			})
			// var cli = this.local.session.getItem("CURRENT");
			// this.houseRentClientVo.clientId = cli.client.clientId;
		},
		beforeRouteLeave(to, from, next) {
			this.$destroy(); //手动销毁当前页面
			next();
		},
		methods: {
			submit: function() {
        if(this.$data.carRentClientVo.tel === ""){
          this.message.error("电话必须输入");
          return;
        }
				if (this.$data.carRentClientVo.title === null) {
					this.message.error("标题必须输入");
					return;
				}

				let self = this;
				this.http.post({
					url: "/car-buy-vo/info",
					param: {
						title: this.$data.carRentClientVo.title,
						details: this.$data.carRentClientVo.detail,
						tel: this.$data.carRentClientVo.tel,
						wxNumber: this.$data.carRentClientVo.wxNumber,
						clientId: this.$data.carRentClientVo.clientId,
					}
				}).then(data => {
					self.message.info("新增成功");
					this.http.put({
						url: "/pay-order/info/caozuo",
						param: {

							orderNumber: this.orderInfo.orderNumber
						}
					})
					this.cancel()

				});
			},
			cancel: function() {
				this.$router.push({
					name: "user_center_car_buy"
				})
			},
			getOrderStatu: function() {
				console.log(this.$data.number);
				this.number--;
				if (this.number === 0) {
					clearInterval(this.longSearch)
					this.carRentClientVo.modal11 = false
					this.number = 10
					this.$Notice.error({
						title: '支付超时',
					});
				}
				//发请求查询支付状态
				this.http.get({
					url: "/pay-order/info",
					param: {

						orderNumber: this.orderInfo.orderNumber
					}
				}).then(data => {

					console.log("支付状态" + data);

					if (data == 2) {
						this.carRentClientVo.modal11 = false
						clearInterval(this.longSearch)
						//this.longSearch()
						this.pay = false
						this.sub = true
						this.$Notice.success({
							title: '支付成功',
						});
					}
				})

			},
			modal: function() {
				clearInterval(this.longSearch)
				console.log(this.$data.orderInfo)
				this.number = 10
				this.carRentClientVo.modal11 = true
				this.carRentClientVo.url = this.iport + 'pay/nativeV2?clientId=' + this.$data.orderInfo
					.clientId +
					'&orderTypeId=' + this.$data.orderInfo.orderTypeId +
					'&money=' + this.$data.orderInfo.price +
					'&orderNumber=' + this.$data.orderInfo.orderNumber
				console.log(this.carRentClientVo.url)
				this.longSearch = setInterval(this.getOrderStatu, 3000)
			}
		}
	}
</script>

<style>
</style>
